/*
*  Copyright (c) 2006-2020, Chukie
*  
*  SPDX-License-Identifier: Apache-2.0
*  
*  @file     : dial_sample.h
*  
*  @brief    : 
*  Website   : https://gitee.com/open-embedded
*  
*  
*  Change Logs:
*  Date           Author          Notes
*  2021-08-16     zhaocj       The first version
*/

/* Define to prevent recursive inclusion ------------------------------------*/
#ifndef __DIAL_SAMPLE_H__
#define __DIAL_SAMPLE_H__
/* Includes -----------------------------------------------------------------*/
#include <rtthread.h>
#include <cellular.h>

#include "lwip/ip_addr.h"
#include "lwip/inet.h"

#ifdef PKG_NETUTILS_NTP
    #include <ntp.h>
    #include <sys/time.h>
#endif /*PKG_NETUTILS_NTP*/

#ifdef SYSTEM_USING_SYSWATCHER
    #include <system.h>
#endif /*SYSTEM_USING_SYSWATCHER*/
#ifdef __cplusplus
extern "C"
{
#endif /*__cplusplus*/
/* Exported types -----------------------------------------------------------*/
typedef enum
{
    DIAL_STATE_START_MODEM = 0,
    DIAL_STATE_INIT_MODEM,
    DIAL_STATE_CHECK_SIM,
    DIAL_STATE_CONFIG,
    DIAL_STATE_CHECK_NETWORK,
    DIAL_STATE_PPP_START,
    DIAL_STATE_PPP_CONNECTING,
    DIAL_STATE_PPP_CONNECTED,
    DIAL_STATE_PPP_DISCONNECTED,
    DIAL_STATE_FAILED,
    DIAL_STATE_WAITTING,
} dial_state_t;

typedef struct dial_instance *dial_instance_t;
struct dial_instance
{
    cellular_device_t device;
    /* 运行时参数******************/
    dial_state_t state;
    /*定时器*/
    time_t timer;
    
    /*PPP建立时间戳*/
    time_t ppp_connected_ts;
    int redial_wait_time;
    
#ifdef PKG_NETUTILS_NTP
    /*ntp对时时间戳*/
    time_t ntp_sync_ts;
    time_t ntp_sync_interval;
    struct _ntp_cfg
    {
        char ntp_server[64];
        bool ntp_enable;
    } ntp_cfg;
#endif /*PKG_NETUTILS_NTP*/
   
};

/* Exported constants -------------------------------------------------------*/
/* Exported macro -----------------------------------------------------------*/
#define DIAL_THREAD_NAME           "dial"
#define DIAL_MAX_ERR_TIMES         10
#define DIAL_ERR_WAIT_TIME         20
#define DIAL_PPP_TIMEOUT           120
#define DEFAULT_DIAL_NUMBER        "*99***1#"
#define DEFAULT_DIAL_APN           ""
#define DEFAULT_DIAL_USERNAME      ""
#define DEFAULT_DIAL_PASSWORD      ""
#define DEFAULT_DIAL_AUTH_TYPE     PPP_AUTH_TYPE_NONE
#define DEFAULT_DIAL_NETWORK_MODE  NETWORK_MODE_AUTO

#define DIAL_THREAD_PRIORITY         (sys_get_app_thread_prio())
#define DIAL_THREAD_STACK_SIZE       3072
#define DIAL_THREAD_TIMESLICE        60

#define DIAL_NTP_SYNC_INTERVAL       3600
#define DIAL_NTP_SYNC_TIMEOUT        30

#define DEFAULT_TIMED_ACTIVATION_INTERVAL  0
#define DEFAULT_TIMED_OFFLINE_INTERVAL     60
#define DEFAULT_PPP_ECHO_INTERVAL          60
#define DEFAULT_REDIAL_INTERVAL            60
#define DEFAULT_REDIAL_MAX_TIMES           3

#define DIAL_MAX_FAILED_TIMES               5
#define DIAL_MAX_SIM_ERROR_TIMES            10

#define DIAL_WAIT_TIME_WHILE_SIM_ERR        20

#define DIAL_WAIT_MODULE_START_TIMEOUT      30
#define DIAL_WAIT_MODULE_INIT_TIMEOUT       20
#define DIAL_WAIT_CHECK_SIM_TIMEOUT         30
#define DIAL_WAIT_MODULE_CONFIG_TIMEOUT     20
#define DIAL_WAIT_CHECK_NETWORK_TIMEOUT     60
#define DIAL_WAIT_PPP_TIMEOUT               60

/* Exported functions -------------------------------------------------------*/


#ifdef __cplusplus
{
#endif /*__cplusplus*/
#endif /*__DIAL_SAMPLE_H__*/
/* End of file****************************************************************/

